package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.dafuweng.DFWUnionData;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionRecord;
import com.yanqu.road.entity.activity.dafuweng.DFWUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.manager.cross.battle.DaFuWengDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossDaFuWengPb {
    /**
     * 家族信息
     *
     * @param dfwUnionData
     * @return
     */
    public static MonopolyProto.UnionDataMsg.Builder getUnionDataMsg(DFWUnionData dfwUnionData) {
        MonopolyProto.UnionDataMsg.Builder builder = MonopolyProto.UnionDataMsg.newBuilder();
        for (Map.Entry<Long, Integer> entry : dfwUnionData.getBuyTimesMap().entrySet()) {
            builder.addBuy(getUnionMoneyPackageBuyMsg(entry.getKey(), entry.getValue()));
        }
        for (Map.Entry<Integer, List<Long>> entry : dfwUnionData.getRebateUserList().entrySet()) {
            builder.addGetReward(getUnionRebateGetRewardRecordMsg(entry.getKey(), entry.getValue()));
        }
        builder.setIronCollectCount(DaFuWengDataMgr.getIronCollectCount(dfwUnionData));
        int openBoxTimes = 0;
        Map<Long, CrossUnionMember> unionMemberMap = DaFuWengDataMgr.getUnionMemberMap(dfwUnionData.getUnionUid());
      /*  for (UnionMember member : unionMemberMap.values()) {
            DFWUserData userData = DaFuWengDataMgr.getUserData(DaFuWengDataMgr.getDfwConfig(dfwUnionData.getActivityId()).getActivityInfo().getActivityId(), member.getUserId());
            openBoxTimes += userData.getBoxOpenTimes();
        }*/
        builder.setOpenBoxCount(openBoxTimes);
        builder.addAllItemList(parseUnionCollectItemMsgList(dfwUnionData));
        builder.setUnionAddition(dfwUnionData.getAddition());
        return builder;
    }

    public static MonopolyProto.UnionMoneyPackageBuyMsg.Builder getUnionMoneyPackageBuyMsg(long userId, int times) {
        MonopolyProto.UnionMoneyPackageBuyMsg.Builder builder = MonopolyProto.UnionMoneyPackageBuyMsg.newBuilder();
        builder.setNickName(CrossUserMgr.getUserBaseInfo(userId).getNickName());
        builder.setBuyTimes(times);
        return builder;

    }

    public static MonopolyProto.UnionRebateGetRewardRecordMsg.Builder getUnionRebateGetRewardRecordMsg(int times, List<Long> userList) {
        MonopolyProto.UnionRebateGetRewardRecordMsg.Builder builder = MonopolyProto.UnionRebateGetRewardRecordMsg.newBuilder();
        builder.setTimes(times);
        for (long userId : userList) {
            builder.addNickName(CrossUserMgr.getUserBaseInfo(userId).getNickName());
        }
        return builder;
    }

    public static List<MonopolyProto.UnionCollectItemMsg> parseUnionCollectItemMsgList(DFWUnionData dfwUnionData) {
        List<MonopolyProto.UnionCollectItemMsg> list = new ArrayList<>();
        Map<Integer, Integer> countMap = dfwUnionData.getCollectCountMap();
        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            MonopolyProto.UnionCollectItemMsg.Builder builder = MonopolyProto.UnionCollectItemMsg.newBuilder();
            builder.setGoodsId(entry.getKey());
            builder.setCount(entry.getValue());
            list.add(builder.build());
        }
        return list;
    }

    public static MonopolyProto.UnionDataMsg.Builder getEmptyUnionDataMsg() {
        MonopolyProto.UnionDataMsg.Builder builder = MonopolyProto.UnionDataMsg.newBuilder();
        builder.setUnionAddition(0);
        builder.setIronCollectCount(0);
        builder.setOpenBoxCount(0);
        return builder;
    }

    public static List<MonopolyProto.UnionRecordMsg> parseUnionRecordMsgList(List<DFWUnionRecord> list) {
        List<MonopolyProto.UnionRecordMsg> msgList = new ArrayList<>();
        for (DFWUnionRecord record : list) {
            MonopolyProto.UnionRecordMsg.Builder builder = MonopolyProto.UnionRecordMsg.newBuilder();
            builder.setMonopolyPlayer(parseMonopolyPlayerMsg(record.getUserId(), record.getUserBaseInfo()));
            builder.setTime(record.getCreateTime());
            builder.setBeatBackTimes(record.getBeatBackTimes());
            builder.setRecordId(record.getRecordId());
            msgList.add(builder.build());
        }
        return msgList;
    }

    public static MonopolyProto.MonopolyPlayerMsg parseMonopolyPlayerMsg(long userId, UserBaseInfo userBaseInfo) {
        MonopolyProto.MonopolyPlayerMsg.Builder builder = MonopolyProto.MonopolyPlayerMsg.newBuilder();
        builder.setPlayer(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        builder.setUserId(userId);
        return builder.build();
    }
}
